package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collection;

@RestController
@RequestMapping("/user")
public class UserController {
    @Reference
    private UserService userService;
    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;
    //获取当前登录（认证）用户的用户名
    /**
     * 获取当前登录（认证）用户的用户名
     * @return
     */
    @RequestMapping("/getLoginUsername")
    public Result getLoginUsername(){
        try{
            User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            String username = user.getUsername();
            String password = user.getPassword();
            Collection<GrantedAuthority> authorities = user.getAuthorities();
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS,username);
        }catch (Exception e){
            return new Result(false, MessageConstant.GET_USERNAME_FAIL);
        }
    }

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = userService.findPage(queryPageBean);
        return pageResult;
    }

    /**
     * 新增用户
     * @param roleId
     * @param user
     * @return
     */
    @RequestMapping("/add")
    public Result add(Integer[] roleId, @RequestBody com.itheima.pojo.User user){
        try {
            String password = bCryptPasswordEncoder.encode(user.getPassword());
            user.setPassword(password);
            userService.add(roleId,user);
            return new Result(true,MessageConstant.ADD_USER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_USER_FAIL);
        }
    }

    /**
     * 编辑用户
     * @param roleId
     * @param user
     * @return
     */
    @RequestMapping("/edit")
    public Result edit(Integer[] roleId, @RequestBody com.itheima.pojo.User user){
        try {
            userService.edit(roleId,user);
            return new Result(true,MessageConstant.EDIT_USER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_USER_FAIL);
        }
    }

    /**
     * 删除用户
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    public Result delete(String id){
        try {
            userService.delete(Integer.parseInt(id));
            return new Result(true,MessageConstant.DELETE_USER_SUCCESS);
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return new Result(false,"删除用户失败");
        }
    }
    @RequestMapping("/findUserById")
    public Result findUserById(String id){
        try {
            com.itheima.pojo.User user=userService.findUserById(Integer.parseInt(id));
            return new Result(true,MessageConstant.QUERY_USER_SUCCESS,user);
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_USER_FAIL);
        }
    }

}
